
package com.scs.application.modules.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.modules.order.entity.DeliveryItem;
import com.scs.application.modules.order.entity.DeliveryItemSn;
import com.scs.application.modules.order.mapper.DeliveryItemSnMapper;
import com.scs.application.modules.order.request.DeliveryItemSnQueryRequest;
import com.scs.application.modules.order.service.DeliveryItemService;
import com.scs.application.modules.order.service.DeliveryItemSnService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * sn生成和查询 controller
 *
 */
@AllArgsConstructor
@RestController
@RequestMapping("/order/delivery_item_sns")
public class DeliveryItemSnController extends CrudController<DeliveryItemSn, String, DeliveryItemSnService> implements ImAndExAbleController<DeliveryItemSnQueryRequest> {
    @Autowired
    @Lazy
    private DeliveryItemService deliveryItemService;
    @Autowired
    @Lazy
    private DeliveryItemSnMapper deliveryItemSnMapper;

    @PostMapping("/{id}")
    public CommonResult saveDeliveryItemSns(@PathVariable("id") String deliveryId) {
        List<DeliveryItemSn> deliveryItemSnList = service.saveDeliveryItemSns(deliveryId, null);
        return CommonResult.ok("新增成功", deliveryItemSnList);
    }

    @GetMapping("/page")
    public PageResult page(DeliveryItemSnQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(DeliveryItemSnQueryRequest request) {
        List<DeliveryItemSn> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    // 打印标签数据
    @GetMapping("/getPrintLabelData/{deliveryId}")
    public CommonResult getPrintLabelData(@PathVariable("deliveryId") String deliveryId) {
        List<DeliveryItem> deliveryItemList = deliveryItemService.list(Wrappers.<DeliveryItem>query().eq("delivery_id",deliveryId));
        List<DeliveryItemSn> deliveryItemSnList = service.saveDeliveryItemSns(deliveryId, null);
        Map result = new HashMap();
        result.put("snList",deliveryItemSnList);
        result.put("deliveryItemList",deliveryItemList);
        return CommonResult.ok("新增成功", result);
    }
}
